package pers.karl.utils.term.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;

public class FileUtils {

    /**
     * 读取文本文件
     * @param filePath
     * @return
     * @throws IOException
     */
    public static String readTextFromFile(String filePath) throws IOException {

        try {
            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            return new String(bytes);
        } catch (NoSuchFileException e) {
            return "{}";
        }

    }

    /**
     * 写入文件
     * @param filePath
     * @param text
     * @throws IOException
     */
    public static void writeTextToFile(String filePath, String text) throws IOException {
        Files.write(Paths.get(filePath), text.getBytes());
    }

}
